package {
import org.flixel.*;

public class GameState extends FlxState {

	private var wallTiles:FlxGroup;
	private var floorTiles:FlxGroup;
	private var objects:FlxGroup;
	private var hazards:FlxGroup;
	private var player:Player;
	private var skeleton:Skeleton;
	private var sword:Sword;
	private var minimap:FlxCamera;

    override public function create(): void {
		// simple grass floor
		floorTiles = new FlxGroup();
		wallTiles = new FlxGroup();
		for (var x:uint = 0; x < FlxG.width; x += Tile.WIDTH) {
			for (var y:uint = 0; y < FlxG.height; y += Tile.HEIGHT) {
				var tile:Tile;
				if (x == 0 || y == 0 || x == FlxG.width - Tile.WIDTH || y == FlxG.height - Tile.HEIGHT) {
					tile = new Tile(x, y, 2);
					wallTiles.add(tile);
				}
				else {
					tile = new Tile(x, y, 0);
					floorTiles.add(tile);
				}
			}
		}
		add(floorTiles);
		add(wallTiles);

		// add the player
		player = new Player(100, 100, 100);
		add(player.getSword());
		add(player);

		// add the skeleton
		skeleton = new Skeleton(200, 200, 0);
		add(skeleton.getBone());
		add(skeleton);

		// basically, all the stuff in the level
		objects = new FlxGroup();
		objects.add(skeleton);
		objects.add(player);
		objects.add(skeleton.getBone());
		objects.add(player.getSword());

		// baddies
		hazards = new FlxGroup();
		hazards.add(skeleton);
		hazards.add(skeleton.getBone());
		
		/* make this awesome later
		minimap = new FlxCamera(FlxG.width, 0, FlxG.width, FlxG.height, 0.5);
		FlxG.addCamera(minimap);
		minimap.target = player; */

		FlxG.mouse.show();
    }

	public function collide(o1: FlxObject, o2: FlxObject): void {
		if (o1 is Sword && o2 is Bone)
			return;
		if (o1 is Bone || o1 is Sword)
			o1.kill();
		o2.hurt(10);
	}
	
	override public function update(): void {
		// fix this broken respawn
		if (!player.alive && FlxG.keys.SPACE)
		{
			trace("wtf");
			//player.x = 100;
			//player.y = 100;
			player.revive();
		}

		player.angle = FlxU.getAngle(player.getMidpoint(), FlxG.mouse.getScreenPosition());

		objects.members.sortOn("y", DESCENDING);

		super.update();
		
		FlxG.collide(wallTiles, objects);
		FlxG.overlap(hazards, player , collide);
		FlxG.overlap(player.getSword(), hazards, collide);
    }
}
}